//
//  LoginBySecretController.m
//  HaiGeEr_Student
//
//  Created by liuchen on 2018/9/9.
//  Copyright © 2018年 海格尔. All rights reserved.
//

#import "LoginBySecretController.h"
#import "LoginResetSecretController.h"
#import "LoginFindSecretController.h"
#import "CoachTabBarController.h"
#import "LoginByCodeController.h"
#import "WKWebController.h"
#import "LoginBySecretView.h"
#import "UserModel.h"

@interface LoginBySecretController ()<UIGestureRecognizerDelegate>
@property (nonatomic,weak) UIButton *loginBtn;
@property (nonatomic,weak) UIButton *logiByCodeBtn;
@property (nonatomic,weak) LoginBySecretView *loginBySecretView;
@end

@implementation LoginBySecretController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setHidden:YES];
    [[CoachTabBarController shareLZHTabbarController].tabBar setHidden:YES];
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setHidden:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createSubView];
}

#pragma mark ==========创建视图==========
- (void)createSubView{
    self.view.backgroundColor = [UIColor whiteColor];
    LoginBySecretView *loginBySecretView = [[LoginBySecretView alloc]initWithFrame:CGRectMake(0, SYSTEM_STATUSBAR_HEGHT+83*SCALEUNIT, [UIScreen mainScreen].bounds.size.width, 515*SCALEUNIT)];
    [loginBySecretView.forgetBtn addTarget:self action:@selector(forgetChange:) forControlEvents:UIControlEventTouchUpInside];
    [loginBySecretView.userAgreementBtn addTarget:self action:@selector(JumpToUserAgreement:) forControlEvents:UIControlEventTouchUpInside];
    _loginBySecretView = loginBySecretView;
    [self.view addSubview:self.loginBySecretView];
    
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    loginBtn.frame = CGRectMake(70*SCALEUNIT, CGRectGetMaxY(self.loginBySecretView.frame)+65*SCALEUNIT, 610*SCALEUNIT, 90*SCALEUNIT);
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginBtn.titleLabel setFont:[UIFont systemFontOfSize:LoginFont]];
    [loginBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [loginBtn setBackgroundColor:LOGINBUTTONBACKRGBA];
    [loginBtn.layer setShadowColor:[UIColor blackColor].CGColor];
    [loginBtn.layer setShadowOffset:CGSizeMake(5*SCALEUNIT, 5*SCALEUNIT)];
    [loginBtn.layer setShadowOpacity:0.8];
    [loginBtn addTarget:self action:@selector(loginIn:) forControlEvents:UIControlEventTouchUpInside];
    _loginBtn = loginBtn;
    [self.view addSubview:self.loginBtn];
    
    UIButton *loginByCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    loginByCodeBtn.frame = CGRectMake(275*SCALEUNIT, CGRectGetMaxY(self.loginBtn.frame)+50*SCALEUNIT, 200*SCALEUNIT, 50*SCALEUNIT);
    [loginByCodeBtn setTitle:@"手机验证码登录" forState:UIControlStateNormal];
    [loginByCodeBtn setTitleColor:LOGINBUTTONBACKRGBA forState:UIControlStateNormal];
    [loginByCodeBtn.titleLabel setFont:[UIFont systemFontOfSize:LoginFont-5]];
    [loginByCodeBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [loginByCodeBtn addTarget:self action:@selector(loginByCodeChange:) forControlEvents:UIControlEventTouchUpInside];
    _logiByCodeBtn = loginByCodeBtn;
    [self.view addSubview:self.logiByCodeBtn];
    
//    UIButton *ceshione = [UIButton buttonWithType:UIButtonTypeCustom];
//    ceshione.frame = CGRectMake(50*SCALEUNIT, CGRectGetMaxY(self.logiByCodeBtn.frame)+50*SCALEUNIT, 200*SCALEUNIT, 50*SCALEUNIT);
//    ceshione.backgroundColor = [UIColor orangeColor];
//    ceshione.layer.masksToBounds = YES;
//    ceshione.layer.cornerRadius = 10;
//    [ceshione setTitle:@"刑凯" forState:UIControlStateNormal];
//    [ceshione setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [ceshione.titleLabel setFont:[UIFont systemFontOfSize:LoginFont-5]];
//    [ceshione setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
//    [ceshione addTarget:self action:@selector(changeIp:) forControlEvents:UIControlEventTouchUpInside];
//    ceshione.tag = 10000;
//    [self.view addSubview:ceshione];
//
//    UIButton *ceshitwo = [UIButton buttonWithType:UIButtonTypeCustom];
//    ceshitwo.frame = CGRectMake(CGRectGetWidth(self.view.frame)-50*SCALEUNIT-200*SCALEUNIT, CGRectGetMaxY(self.logiByCodeBtn.frame)+50*SCALEUNIT, 200*SCALEUNIT, 50*SCALEUNIT);
//    ceshitwo.backgroundColor = [UIColor orangeColor];
//    ceshitwo.layer.masksToBounds = YES;
//    ceshitwo.layer.cornerRadius = 10;
//    ceshitwo.tag = 10000+1;
//    [ceshitwo setTitle:@"测试" forState:UIControlStateNormal];
//    [ceshitwo setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [ceshitwo.titleLabel setFont:[UIFont systemFontOfSize:LoginFont-5]];
//    [ceshitwo setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
//    [ceshitwo addTarget:self action:@selector(changeIp:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:ceshitwo];
}

//- (void)changeIp:(UIButton *)btn{//1-刑凯 2-李荣晨 0-测试
//    NSInteger judge = 0;
//    if (btn.tag-10000 == 0) {
//        judge = 1;
//    } else if (btn.tag-10000 == 1){
//        judge = 2;
//    }
//    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInteger:judge] forKey:@"IPWEB"];
//    [[NSUserDefaults standardUserDefaults] synchronize];
//    [self showError:[NSString stringWithFormat:@"%@IP设置成功",judge==1?@"刑凯":@"测试"]];
//}

#pragma mark ==========登录==========
- (void)loginIn:(UIButton *)btn{
    if ([self.loginBySecretView.inputView.textField.text isEqualToString:@""]) {
        [self showMessage:@"请输入手机号"];
    }else if ([self.loginBySecretView.inputHideView.textField.text isEqualToString:@""]) {
        [self showMessage:@"请输入登录密码"];
    }else if (!LOGINPHONECHECK(self.loginBySecretView.inputView.textField.text)) {
        [self showMessage:@"手机号格式错误"];
    }else if (!self.loginBySecretView.selectJudge) {
        [self showMessage:@"请勾选用户协议"];
    }else {
        [self showLoading];
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
                             self.loginBySecretView.inputView.textField.text, @"phone",
                             BASE64(self.loginBySecretView.inputHideView.textField.text), @"code",
                             @"2", @"loginType", nil];
        [[NetworkEngine sharedClient] GET:LGGINBYPASSWORD data:dic success:^(id responseObject) {
            [self hideHUD];
            if ([[responseObject objectForKey:@"code"] integerValue]==1) {
                [UserMessage sharedInstance].model = [UserModel mj_objectWithKeyValues:[responseObject objectForKey:@"data"]];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"userMessage"];
                [[NSUserDefaults standardUserDefaults] setObject:[responseObject objectForKey:@"data"] forKey:@"userMessage"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                [self jumpToMainPage];
            } else {
                [self showMessage:[responseObject objectForKey:@"msg"]];
            }
        } failure:^(NSString *error) {
            [self showError:error];
        }];
    }
}
- (void)jumpToMainPage{
    if ([[UserMessage sharedInstance].model.loginTime length]==0) {
        LoginResetSecretController *vc = [[LoginResetSecretController alloc] init];
        vc.resetJudge = YES;
        vc.phone = self.loginBySecretView.inputView.textField.text;
        [self toNextPageWithAnimation:vc];
    }else {
        [[[UIApplication sharedApplication].delegate window].rootViewController removeFromParentViewController];
        [[UIApplication sharedApplication].delegate window].rootViewController = [CoachTabBarController shareLZHTabbarController];
        [[[UIApplication sharedApplication].delegate window] makeKeyAndVisible];
    }
}

#pragma mark ==========忘记密码界面==========
- (void)forgetChange:(UIButton *)btn{
    [self toNextPageWithAnimation:[[LoginFindSecretController alloc]init]];
}

#pragma mark ==========验证码登录界面==========
- (void)loginByCodeChange:(UIButton *)btn{
    [self toNextPageWithAnimation:[[LoginByCodeController alloc] init]];
}

#pragma mark ==========用户协议==========
- (void)JumpToUserAgreement:(UIButton *)btn{
    WKWebController *wkWeb = [[WKWebController alloc]init];
    wkWeb.titleConten = @"用户协议";
    wkWeb.url = [[NSBundle mainBundle] URLForResource:@"UserProtocol.html" withExtension:nil];
    [self.navigationController pushViewController:wkWeb animated:YES];
}

#pragma mark ==========动画效果==========
- (void)toNextPageWithAnimation:(UIViewController *)vc{
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark ==========禁止右滑返回上一层==========
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer
                                      *)gestureRecognizer{
    return NO; //YES：允许右滑返回 NO：禁止右滑返回
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
